﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.RF.ProductCategory;
using Model;
using Model.RF.ProductCategory;

public partial class ProductManage_RFProductManage_Manage_ProductType_MultiBandManage : System.Web.UI.Page
{
    private ProductType_MultiBand pm;
    public string seachFactor
    {
        get { return ViewState["SeachFactor"].ToString(); }
        set { ViewState["SeachFactor"] = value; }
    }

    public ProductManage_RFProductManage_Manage_ProductType_MultiBandManage()
    {
        pm = new ProductType_MultiBand();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            seachFactor = string.Empty;
            BindGV();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        seachFactor = this.TextBox1.Text.Trim();
        BindGV();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        seachFactor = string.Empty;
        BindGV();
    }

    protected void BindGV()
    {
        if (string.IsNullOrEmpty(seachFactor))
            this.GridView1.DataSource = pm.LoadEntities();
        else
            GridView1.DataSource = pm.ProductType_MultiBand_GetbyPID(seachFactor);
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindGV();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[1].ToString());
        pm.Delete(id);
        BindGV();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ProductType_MultiBandInfo productTypeInfo = new ProductType_MultiBandInfo();
        productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.StepItem = ((DropDownList)(d.Rows[1].Cells[1].FindControl("DDLStepItem"))).SelectedValue;
        productTypeInfo.TestBand = ((DropDownList)(d.Rows[2].Cells[1].FindControl("DDLTestBand"))).SelectedValue;

        ProductType p = new ProductType();
        ProductTypeInfo pi = p.LoadEntity(productTypeInfo.ProductTypeID);
        if (string.IsNullOrEmpty(pi.ProductTypeID))
        {
            string str1 = "此产品类型不存在，必须是在产品类型信息中录入过的产品！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            pm.Insert(productTypeInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindGV();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindGV();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
